package mpcs.view
{
	import mpcs.NotiConst;
	import mpcs.model.vo.GlobalVO;
	import mpcs.utils.TrimUtils;
	import mpcs.view.windows.HistoryMDIWindow;
	
	import mx.controls.Alert;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import spark.utils.DataItem;
	
	/**
	 *  
	 * @author zhangzuoqiang
	 */	
	public class HistoryMediator extends Mediator {
		
		public static const NAME:String = "HistoryMediator";
		
		public function HistoryMediator( viewComponent:HistoryMDIWindow ) {
			super(NAME, viewComponent);
		}
		
		override public function listNotificationInterests():Array {
			return [
				NotiConst.CAN_S_POSITION_LIST, 
				NotiConst.R_POSITION_LIST_SUCCESS, 
				NotiConst.R_POSITION_LIST_FAILED,
				NotiConst.POSITION_LIST_CHANGE
			];
		}
		
		override public function handleNotification( note:INotification ):void {
			switch ( note.getName() ) {
				case NotiConst.CAN_S_POSITION_LIST:
					// 可以发送请求历史数据
					trace("可以发送请求历史数据 ... ");
					sendNotification( NotiConst.S_POSITION_LIST );
					break;
				case NotiConst.R_POSITION_LIST_SUCCESS:
					// 请求历史数据成功
					trace("请求历史数据成功 ... ");
					historyWin.dataArr = GlobalVO.userVO.getPositonList();
					break;
				case NotiConst.R_POSITION_LIST_FAILED:
					// 请求历史数据失败
					Alert.show( "错误代码: " + note.getBody() , "请求历史数据失败");
					break;
				case NotiConst.POSITION_LIST_CHANGE:
					// 有新的位置更新
					trace("有新的位置更新 ... HistoryMediator ... ");
					var item:DataItem = new DataItem();
					item.date = note.getBody().tsp;
					item.longitude = note.getBody().lng;
					item.latitude = note.getBody().lat;
					GlobalVO.userVO.getPositonList().addItemAt(item , GlobalVO.userVO.getPositonList().length);
					historyWin.dataArr = GlobalVO.userVO.getPositonList();
					break;
			}
		}
		
		private function get historyWin ():HistoryMDIWindow {
			return viewComponent as HistoryMDIWindow;
		}
		
	}
}